/*
 * =====================================================================================
 *
 *       Filename:  myt.cc
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2009年11月12日 13时20分07秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  lerosua (), lerosua@gmail.com
 *        Company:  cyclone
 *
 * =====================================================================================
 */


#include <cassert>
#include <sstream>
#include <string>
#include <fstream>
#include <iostream>

using namespace std;

/** get status create time */
std::string getSecTime(const std::string& input);
std::string getCreateTime(const std::string& input);
std::string getStatusText(const std::string& input);
std::string getScreenName(const std::string& input);
std::string getImageUrl(const std::string& input);
std::string getTimeZone(const std::string& input);
std::string getSource(const std::string& input);
std::string getFollowerC(const std::string& input);
std::string getFriendsC(const std::string& input);

int num=1;

void parse(const std::string& input)
{
	std::string str;

	cout<<"第 "<<num<<endl;
	num++;
	str=getSecTime(input);
	cout<<str<<endl;
	str=getStatusText(input);
	cout<<str<<endl;
	str=getScreenName(input);
	cout<<str<<endl;
	str=getImageUrl(input);
	cout<<str<<endl;
	str=getTimeZone(input);
	cout<<str<<endl;
	str=getSource(input);
	cout<<str<<endl;
	str=getFollowerC(input);
	cout<<"Follower "<<str<<endl;
	str=getFriendsC(input);
	cout<<"Friends "<<str<<endl;
	cout<<endl;
}

std::string getImageUrl(  const std::string& input)
{
	string text_str;
	string tmp;
	size_t pos = input.find(",\"profile_image_url\":\"");
	size_t t_pos;
	if(pos!=std::string::npos){
		tmp=input.substr(pos+22,std::string::npos);	
		
		t_pos = tmp.find("\"");
		text_str=tmp.substr(0,t_pos);
		//cout<<tmp<<endl;
		//cout<<text_str<<endl;
		return text_str;
	}
	return "";

}
std::string getStatusText(const std::string& input)
{
	string text_str;
	string tmp;
	size_t pos = input.find(",\"text\":");
	size_t t_pos;
	size_t tpos;
	if(pos!=std::string::npos){
		tmp=input.substr(pos+9,std::string::npos);	
		
		t_pos = tmp.find("\"}");
		text_str=tmp.substr(0,t_pos);
		tpos=text_str.find("\",\"");
		if(tpos!=std::string::npos){
			return text_str.substr(0,tpos);
		}else
			return text_str;

	}
	return "";
}
std::string getScreenName(const std::string& input)
{
	string text_str;
	string tmp;
	size_t pos = input.find(",\"screen_name\":\"");
	size_t t_pos;
	if(pos!=std::string::npos){
		tmp=input.substr(pos+16,std::string::npos);	
		
		t_pos = tmp.find("\"");
		text_str=tmp.substr(0,t_pos);
		//cout<<tmp<<endl;
		//cout<<text_str<<endl;
		return text_str;
	}
	return "";
}

std::string getTimeZone(const std::string& input)
{
	string time_str;
	string tmp;
	size_t pos = input.find("\"time_zone\":\"");
	size_t t_pos;
	if(pos!=std::string::npos){
		tmp=input.substr(pos+13,std::string::npos);	
		
		t_pos = tmp.find("\",\"");
		time_str=tmp.substr(0,t_pos);
		return time_str;
	}
	return "";
}


std::string getSecTime(const std::string& input)
{
	size_t pos = input.find("\"created_at\":");
	string tmp;
	if(pos!=std::string::npos){
		tmp=input.substr(pos+14,std::string::npos);	
		return getCreateTime(tmp);
	}
	return getCreateTime(input);


}
std::string getCreateTime(const std::string& input)
{
	string time_str;
	string tmp;
	size_t pos = input.find("\"created_at\":");
	size_t t_pos;
	if(pos!=std::string::npos){
		tmp=input.substr(pos+14,std::string::npos);	
		
		t_pos = tmp.find("\"");
		time_str=tmp.substr(0,t_pos);
		//return time_str.substr(4,12);
		return time_str;
	}
	return "";
}
std::string getFriendsC(const std::string& input)
{
	string time_str;
	string tmp;
	size_t pos = input.find("\"friends_count\":");
	size_t t_pos;
	if(pos!=std::string::npos){
		tmp=input.substr(pos+16,std::string::npos);	
		
		t_pos = tmp.find(",\"");
		time_str=tmp.substr(0,t_pos);
		return time_str;
	}
	return "";
}
std::string getFollowerC(const std::string& input)
{
	string time_str;
	string tmp;
	size_t pos = input.find("\"followers_count\":");
	size_t t_pos;
	if(pos!=std::string::npos){
		tmp=input.substr(pos+18,std::string::npos);	
		
		t_pos = tmp.find(",\"");
		time_str=tmp.substr(0,t_pos);
		return time_str;
	}
	return "";
}
std::string getSource(const std::string& input)
{
	string time_str;
	string tmp;
	size_t pos = input.find("\"source\":");
	size_t t_pos;
	if(pos!=std::string::npos){
		tmp=input.substr(pos+10,std::string::npos);	
		
		//t_pos = tmp.find("\"");
		t_pos = tmp.find("\",\"");
		time_str=tmp.substr(0,t_pos);
		size_t h_pos=time_str.find("\">");
		if(h_pos == std::string::npos)
			return time_str;
		else{
			std::string out = time_str.substr(h_pos+2,std::string::npos);
			size_t a_pos=out.find("</a>");
			return out.substr(0,a_pos);

		}
	}
	return "";


}
int parseTime(const std::string input)
{
//Tue Dec 08 12:49:09 +0000 2009
	string mon_ ;
	string day_;
	string hour_;
	string min_;
	string year_;

	int x=7;
	mon_ = input.substr(4,x);
	x+=1;
	day_ = input.substr(x,x+2);
	x+=3;
	hour_ = input.substr(x,x+2);
	x+=3;
	min_ = input.substr(x,x+2);
	x+=10;
	year_ = input.substr(x,x+4);


}

int parseTime(const std::string input)
{
//Tue Dec 08 12:49:09 +0000 2009
	string mon_ ;
	string day_;
	string hour_;
	string min_;
	string year_;

	size_t x=4;
	mon_ = input.substr(x,3);
	x+=4;
	day_ = input.substr(x,2);
	x+=3;
	hour_ = input.substr(x,2);
	x+=3;
	min_ = input.substr(x,2);
	x+=12;
	year_ = input.substr(x,4);


	string dot="-";
	cout<<year_<<dot<<mon_<<dot<<day_<<dot<<hour_<<dot<<min_<<endl;
}



int main(int argc, char *argv[])
{

	string filename="./test.json";
	ifstream infile;
	string str_content;
	infile.open(filename.c_str());

	while(getline(infile,str_content)){
		if(str_content.empty())
			continue;


		
	string strp;
	string tmp;
	size_t begin_pos=0;
	size_t pos;
	int count=0;
do{
	pos= str_content.find(",{");
	if(pos==std::string::npos)
	{
		parse(tmp);
		break;
	}

	if(0==count)
		strp=str_content.substr(1,pos-1);
	else
		strp=str_content.substr(begin_pos,pos);
	tmp=str_content.substr(pos+1,std::string::npos);

	parse(strp);
	//cout<<"str"<<count<<" =  "<<strp<<endl<<endl;
	count++;

	str_content=tmp;
	//begin_pos=pos+1;

}while(true);
		parse(tmp);

	//cout<<"str"<<count<<" =  "<<tmp<<endl<<endl;

}







 
	printf("\n");
	return 0;
}
